﻿#region LicenseInfo
/*
 * WoW Automation
 * Copyright 2008, 2009
 * Michael Sutton
 * 
 * This file is part of Wow Automation.
 * 
 * Wow Automation is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Wow Automation is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Wow Automation.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Runtime.InteropServices;

namespace Automation
{
    class sendInput
    {
        public class SendInputClass
        {

            //C# signature for "SendInput()"
            [DllImport("user32.dll", EntryPoint = "SendInput", SetLastError = true)]
            static extern uint SendInput(
                uint nInputs,
                INPUT[] pInputs,
                int cbSize);

            //C# signature for "GetMessageExtraInfo()"
            [DllImport("user32.dll", EntryPoint = "GetMessageExtraInfo", SetLastError = true)]
            static extern IntPtr GetMessageExtraInfo();

            private enum InputType
            {
                INPUT_MOUSE = 0,
                INPUT_KEYBOARD = 1,
                INPUT_HARDWARE = 2,
            }

            [Flags]
            private enum MOUSEEVENTF
            {
                MOVE = 0x0001,  // mouse move 
                LEFTDOWN = 0x0002,  // left button down
                LEFTUP = 0x0004,  // left button up
                RIGHTDOWN = 0x0008,  // right button down
                RIGHTUP = 0x0010,  // right button up
                MIDDLEDOWN = 0x0020,  // middle button down
                MIDDLEUP = 0x0040,  // middle button up
                XDOWN = 0x0080,  // x button down 
                XUP = 0x0100,  // x button down
                WHEEL = 0x0800,  // wheel button rolled
                VIRTUALDESK = 0x4000,  // map to entire virtual desktop
                ABSOLUTE = 0x8000,  // absolute move
            }

            [Flags]
            private enum KEYEVENTF
            {
                EXTENDEDKEY = 0x0001,
                KEYUP = 0x0002,
                UNICODE = 0x0004,
                SCANCODE = 0x0008,
            }

            [StructLayout(LayoutKind.Sequential)]
            private struct MOUSEINPUT
            {
                public int dx;
                public int dy;
                public int mouseData;
                public int dwFlags;
                public int time;
                public IntPtr dwExtraInfo;
            }

            [StructLayout(LayoutKind.Sequential)]
            private struct KEYBDINPUT
            {
                public short wVk;
                public short wScan;
                public int dwFlags;
                public int time;
                public IntPtr dwExtraInfo;
            }

            [StructLayout(LayoutKind.Sequential)]
            private struct HARDWAREINPUT
            {
                public int uMsg;
                public short wParamL;
                public short wParamH;
            }

            [StructLayout(LayoutKind.Explicit)]
            private struct INPUT
            {
                [FieldOffset(0)]
                public int type;
                [FieldOffset(4)]
                public MOUSEINPUT mi;
                [FieldOffset(4)]
                public KEYBDINPUT ki;
                [FieldOffset(4)]
                public HARDWAREINPUT hi;
            }


            // This function simulates a simple mouseclick at the current cursor position.
            public static uint Click()
            {
                INPUT input_down = new INPUT
                                       {
                                           mi =
                                               {
                                                   dx = 0,
                                                   dy = 0,
                                                   mouseData = 0,
                                                   dwFlags = ((int) MOUSEEVENTF.LEFTDOWN)
                                               }
                                       };


                INPUT input_up = input_down;
                input_up.mi.dwFlags = (int)MOUSEEVENTF.LEFTUP;

                INPUT[] input = { input_down, input_up };

                return SendInput(2, input, Marshal.SizeOf(input_down));
            }


        
        
        
        }   

    }
}
